package org.fireflow2.designer.eclipse.figures;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.fireflow.pdl.fpdl20.diagram.basic.Rectangle;
import org.fireflow.pdl.fpdl20.process.Router;
import org.fireflow.pdl.fpdl20.process.features.Feature;
import org.fireflow.pdl.fpdl20.process.features.router.impl.AndJoinAndSplitRouterFeature;
import org.fireflow.pdl.fpdl20.process.features.router.impl.CustomizedRouterFeature;
import org.fireflow.pdl.fpdl20.process.features.router.impl.DynamicRouterFeature;
import org.fireflow.pdl.fpdl20.process.features.router.impl.OrJoinOrSplitRouterFeature;
import org.fireflow2.designer.eclipse.Activator;
import org.fireflow2.designer.eclipse.modelwrapper.basic.RouterWrapper;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;

public class RouterFigure extends Label implements CommonFigureInterface {
	RouterWrapper routerWrapper = null;
	public static final int IconTextGap = 5;
	public static final int MaxTextHeight = 20;
	public static final int MaxTextWidth = 50;
	
	static Map<String,Image> imageFileMap = new HashMap<String,Image>();
	{
		ImageDescriptor imgDesc = ImageDescriptor.createFromFile(Activator.class,"resources/obj32/empty_gateway.png");
		imageFileMap.put("EMPTY_GATEWAY", imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/parallel_gateway.png");
		imageFileMap.put(AndJoinAndSplitRouterFeature.class.getName(), imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/exclusive_gateway_2.jpg");
		imageFileMap.put(OrJoinOrSplitRouterFeature.class.getName(), imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/inclusive_gateway.png");
		imageFileMap.put(DynamicRouterFeature.class.getName(),  imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/complex_gateway.png");
		imageFileMap.put(CustomizedRouterFeature.class.getName(), imgDesc.createImage());
		
	}
	
	/**
	 * 相对位置
	 */
	public org.eclipse.draw2d.geometry.Rectangle getBox(){
		org.eclipse.draw2d.geometry.Rectangle rectangle = new org.eclipse.draw2d.geometry.Rectangle(this.getIconBounds());
		return rectangle;
	}
	
	public org.eclipse.draw2d.geometry.Rectangle getSelectionHandleBounds(){
		org.eclipse.draw2d.geometry.Rectangle rectangle = getBox();
		this.translateToAbsolute(rectangle);
		rectangle.translate(CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE);
		rectangle.resize(CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE);
		return rectangle;
	}
	
	protected boolean useLocalCoordinates() {
		return true;
	}

	public RouterFigure(RouterWrapper wrapper){
		routerWrapper = wrapper;
		
		Rectangle theCircleShape = (Rectangle)routerWrapper.getDiagramElement().getShape();
		
		this.setOpaque(false);
		
		this.setText(getDisplayName());
		if (theCircleShape!=null){
			this.setFont(Util4Eclipse.getFont(theCircleShape.getTitleLabel()));
		}		
		this.setTextPlacement(PositionConstants.SOUTH);
		this.setTextAlignment(CENTER);
		this.setIconAlignment(CENTER);
		this.setIconTextGap(IconTextGap);
		this.setIcon(imageFileMap.get("EMPTY_GATEWAY"));

	}
	public String getDisplayName(){
		Rectangle theShape = (Rectangle)routerWrapper.getDiagramElement().getShape();
		String displayName = routerWrapper.getDisplayName();

		if (theShape!=null && !org.fireflow.pdl.fpdl20.diagram.basic.Label.CONTENT_FROM_WORKFLOW_ELEMENT.equals(theShape.getTitle())){
			displayName = theShape.getTitle();
		}
		return displayName;
	}
	
	public void refreshFigure(){
		Rectangle theShape = (Rectangle)routerWrapper.getDiagramElement().getShape();
		Router theNode = (Router)routerWrapper.getWorkflowModelElement();

		this.setText(getDisplayName());
		this.setFont(Util4Eclipse.getFont(theShape.getTitleLabel()));

		if (theNode!=null){
			Feature f = theNode.getFeature();
			Image image = imageFileMap.get("EMPTY_GATEWAY");
			if (f==null ){
				image = imageFileMap.get("EMPTY_GATEWAY");
			}
			else if (f instanceof AndJoinAndSplitRouterFeature){
				image = imageFileMap.get(AndJoinAndSplitRouterFeature.class.getName());
			}
			else if (f instanceof OrJoinOrSplitRouterFeature){
				image = imageFileMap.get(OrJoinOrSplitRouterFeature.class.getName());
			}
			else if (f instanceof DynamicRouterFeature){
				image = imageFileMap.get(DynamicRouterFeature.class.getName());
			}
			else if (f instanceof CustomizedRouterFeature){
				image = imageFileMap.get(CustomizedRouterFeature.class.getName());
			}

			if (image!=null){
				this.setIcon(image);
			}
		}
		
		repaint();
	}



}
